<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/12/19
 * Time: 21:47
 */
namespace App\Http\Controllers\BackendApi;

use App\Http\Requests\BackendApi\UploadRequest;
use App\Services\BackendApi\UploadService;

class UploadController extends ApiController
{
    protected $uploadService;

    public function __construct(UploadService $uploadService)
    {
        $this->uploadService = $uploadService;
    }

    public function upload(UploadRequest $uploadRequest)
    {
        if (empty($uploadRequest->storage_type)) {
            $storageType = 'alioss';
        } else {
            $storageType = $uploadRequest->storage_type;
        }
        $data = $this->uploadService->uploadFile($uploadRequest->file, $uploadRequest->folder,
            $uploadRequest->old_file_path, $storageType);
        if (is_object($data)) return $data;
        return $this->responseJson(0, ['url' => $data]);
    }

}